package com.xywy.tangandroid.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.TextView;

import com.xywy.tangandroid.R;
import com.xywy.tangandroid.activity.BloodDataDetailAct;
import com.xywy.tangandroid.base.BasicAdapter;
import com.xywy.tangandroid.bean.BloodData;
import com.xywy.tangandroid.receiver.BroadcastAction;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.DateUtil;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;
import com.xywy.tangandroid.vollery.PostRequest;

/**
 * *****************************************
 * 
 * @Title: BloodDataListAdapter.java
 * @Package com.xywy.tangandroid.adapter
 * @Description: TODO(获取血糖数据)
 * @author smz
 * @date 2014-12-5 上午10:25:09
 * @version V1.0
 ******************************************* 
 */
public class BloodDataListAdapter extends BasicAdapter<BloodData> {
	private List<BloodData> list;
	private LayoutInflater inflater;

	public BloodDataListAdapter(Context context, List<BloodData> list) {
		super(context, list);
		this.list = list;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		viewholder holder;
		if (convertView == null) {
			holder = new viewholder();
			convertView = inflater.inflate(R.layout.item_blood_data_list_item, null);
			holder.blood_text = (TextView) convertView.findViewById(R.id.blood_text);
			holder.mouth = (TextView) convertView.findViewById(R.id.mouth);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			convertView.setTag(holder);
		} else {
			holder = (viewholder) convertView.getTag();
		}

		String value = list.get(position).getCandyvalue();
		String time_value = list.get(position).getRecordtime();
		System.out.println("获取的时间..." + time_value);
		String Mouth = DateUtil.getMouthAndDay(Long.valueOf(time_value) * 1000 + "");
		String times = DateUtil.getHourAndMinute(Long.valueOf(time_value) * 1000 + "");

		holder.blood_text.setText(value + "  mmol/L");
		holder.mouth.setText(Mouth);
		holder.time.setText(times);

		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, BloodDataDetailAct.class);
				intent.putExtra("candvalue", list.get(position).getCandyvalue());
				intent.putExtra("device_name", list.get(position).getInputed_device_str());
				intent.putExtra("recordtime", list.get(position).getRecordtime());
				intent.putExtra("shou_data", list.get(position).getShou_data());
				intent.putExtra("period", list.get(position).getPeriod());
				context.startActivity(intent);
			}
		});

		convertView.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {

				createDialog(position, list);

				return false;
			}
		});
		return convertView;
	}

	public class viewholder {
		TextView blood_text;
		TextView mouth;
		TextView time;
	}

	private void createDialog(final int position, final List<BloodData> list) {
		Dialog dialog = new AlertDialog.Builder(context).setTitle("提示").setMessage(R.string.delete_blood_data).setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Intent it = new Intent(BroadcastAction.ACCTION_DELETE_BLOOD_DATA);
				it.putExtra("data",list.get(position));
				it.putExtra("position",position);
				context.sendBroadcast(it);
//				Constant.datatime = list.get(position).getRecordtime();
//				Constant.app_data = list.get(position).getApp_data();
//				Constant.shou_data = list.get(position).getShou_data();

			}
		}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		}).create();
		// 显示提示框
		dialog.show();
	}
}
